package com.bank.ui.admin;

import com.bank.dao.CustomerDAO;
import com.bank.entity.Customer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class AddCustomerDialog extends JDialog {
    private JTextField nameField;
    private JTextField pidField;
    private JTextField phoneField;
    private JTextField addressField;
    private Runnable refreshCallback;

    public AddCustomerDialog(Runnable refreshCallback) {
            this.refreshCallback = refreshCallback;
            setTitle("增加客户");
            setSize(350, 300); // 增加高度以适应新布局
            setModal(true);
            setLocationRelativeTo(null);
            setLayout(new BorderLayout()); // 改为BorderLayout

            // 创建主内容面板
            JPanel contentPanel = new JPanel();
            contentPanel.setLayout(new GridLayout(5, 2, 10, 10)); // 使用GridLayout布局
            contentPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

            // 添加字段组件
            contentPanel.add(new JLabel("客户姓名:"));
            nameField = new JTextField();
            contentPanel.add(nameField);

            contentPanel.add(new JLabel("身份证号:"));
            pidField = new JTextField();
            contentPanel.add(pidField);

            contentPanel.add(new JLabel("联系电话:"));
            phoneField = new JTextField();
            contentPanel.add(phoneField);

            contentPanel.add(new JLabel("联系地址:"));
            addressField = new JTextField();
            contentPanel.add(addressField);

            // 添加空白标签占位
            contentPanel.add(new JLabel(""));
            contentPanel.add(new JLabel(""));

            add(contentPanel, BorderLayout.CENTER);

            // 按钮面板 - 修复布局
            JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
            JButton saveButton = new JButton("保存");
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 1. 获取输入值
                String name = nameField.getText().trim();
                String pid = pidField.getText().trim();
                String phone = phoneField.getText().trim();
                String address = addressField.getText().trim();
                // 2. 非空验证
                if (name.isEmpty() || pid.isEmpty() || phone.isEmpty() || address.isEmpty()) {
                    JOptionPane.showMessageDialog(AddCustomerDialog.this,
                            "请填写完整信息！", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                // 验证身份证号格式 (18位)
                if (pid.length() != 18 || !pid.matches("\\d{17}[0-9Xx]")) {
                    JOptionPane.showMessageDialog(AddCustomerDialog.this,
                            "身份证号格式不正确！必须是18位数字（最后一位可以是X）", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                // 验证电话号码格式
                if (!phone.matches("1[3-9]\\d{9}")) {
                    JOptionPane.showMessageDialog(AddCustomerDialog.this,
                            "电话号码格式不正确！必须是11位手机号", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                try {
                    CustomerDAO customerDAO = new CustomerDAO();

                    //  身份证查重验证
                    if (customerDAO.getCustomerByPID(pid) != null) {
                        JOptionPane.showMessageDialog(AddCustomerDialog.this,
                                "该身份证号已存在！", "错误", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    // 创建客户对象
                    Customer customer = new Customer();
                    customer.setCustomerName(name);
                    customer.setPID(pid);
                    customer.setTelephone(phone);
                    customer.setAddress(address);

                    // 执行DAO插入，添加客户
                    customerDAO.addCustomer(customer);
                    // 成功提示
                    JOptionPane.showMessageDialog(AddCustomerDialog.this,
                            "客户添加成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                    dispose();

                    // 执行刷新回调（更新主界面列表）
                    if (refreshCallback != null) {
                        refreshCallback.run();
                    }// 10. 异常处理
                } catch (SQLException ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(AddCustomerDialog.this,
                            "客户添加失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        JButton cancelButton = new JButton("取消");
        cancelButton.addActionListener(e -> dispose());

        buttonPanel.add(saveButton);
        buttonPanel.add(cancelButton);
        add(buttonPanel, BorderLayout.SOUTH);
    }
}